﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class agents
    {

        private id3 _id3;
        private Overmind _omind;
        private int _startlistsize;
        private List<dataset> _datalist = new List<dataset>();
        private life l = life.high;
        private hunger h = hunger.low;
        static private Random Rand = new Random();
        public Perf p = new Perf();
        private id3 _id;
        private bool is_alive = true;

        public void setDebug(bool b)
        {
            this._id.debug = b;
        }

        public agents(Overmind omind)
        {
            List<dataset> overdata;
            int i;

            this._omind = omind;
            overdata = this._omind.Datalist;
            for (i = 0; i < (overdata.Count - 1); ++i)
                this._datalist.Add(overdata[i]);
            this._startlistsize = this._datalist.Count;

           this._id = new id3(_datalist);
        }

        public void apply_id3()
        {

            dataset d = new dataset(l, (allies)(Rand.Next() % 4), (field)(Rand.Next() % 3), (enemylevel)(Rand.Next() % 4), h, action.fight);

            if (this.h == hunger.high)
            {

                d.Result = environnement.whathappened(d);
                if (this._id.debug)
                {
                    d.Display();
                    Console.Write("Hunger way ---> ");
                }
            }
            else
            {
                //Prevents errors delete when id3.fonctiondestart will be implemented
                
                
                result r = this._id.doId3(d);

                if (r == result.good)
                    d.Result = environnement.whathappened(d);
                else
                {
                    if (this._id.debug)
                        Console.WriteLine("Agent [RUN]");
                    d.Action = action.run;
                    d.Result = result.neutral;
                    p._Nbrun += 1;
                    if (this.h == hunger.low)
                        this.h = hunger.medium;
                    else if (h == hunger.medium)
                        this.h = hunger.high;
                }
            }
            if (d.Result != result.neutral)
                this._datalist.Add(d);
            if (d.Result == result.bad)
            {
                if (this._id.debug)
                    Console.Write("Agent [HURT]");
                if (l == life.low)
                    this.agentdeath();
                else
                {
                    if (l == life.high)
                        l = life.medium;
                    else
                        l = life.low;
                    if (this._id.debug)
                        Console.WriteLine("");
                }
            }
             
            if (d.Result == result.good)
            {
                if (this._id.debug)
                    Console.WriteLine("Agent [FEED]");
                p._NbMobKill += 1;
                l = life.high;
                h = hunger.low;
            }

            p._lifeTime += 1;

        }

        public bool isAlive()
        {
            return this.is_alive;
        }

        public void agentdeath()
        {
            if (this._id.debug)
                Console.WriteLine(" --> I'm Dead sir");
            this._omind.addperf(p);
            this._omind.combinedatalist(_startlistsize, _datalist);
            this.is_alive = false;
        }
    }
}
